home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 365_02 / regexp.c < prev    next >
C/C++ Source or Header  |  1992-04-04  |  20KB  |  935 lines

  1. /* regexp.c */
  2.  
  3. /* This file contains the code that compiles regular expressions and executes
  4.  * them.  It supports the same syntax and features as vi's regular expression
  5.  * code.  Specifically, the meta characters are:
  6.  *    ^    matches the beginning of a line
  7.  *    $    matches the end of a line
  8.  *    \<    matches the beginning of a word
  9.  *    \>    matches the end of a word
  10.  *    .    matches any single character
  11.  *    []    matches any character in a character class
  12.  *    \(    delimits the start of a subexpression
  13.  *    \)    delimits the end of a subexpression
  14.  *    *    repeats the preceding 0 or more times
  15.  * NOTE: You cannot follow a \) with a *.
  16.  *
  17.  * The physical structure of a compiled RE is as follows:
  18.  *    - First, there is a one-byte value that says how many character classes
  19.  *      are used in this regular expression
  20.  *    - Next, each character class is stored as a bitmap that is 256 bits
  21.  *      (32 bytes) long.
  22.  *    - A mixture of literal characters and compiled meta characters follows.
  23.  *      This begins with M_BEGIN(0) and ends with M_END(0).  All meta chars
  24.  *      are stored as a \n followed by a one-byte code, so they take up two
  25.  *      bytes apiece.  Literal characters take up one byte apiece.  \n can't
  26.  *      be used as a literal character.
  27.  *
  28.  * If NO_MAGIC is defined, then a different set of functions is used instead.
  29.  * That right, this file contains TWO versions of the code.
  30.  */
  31.  
  32. #include <setjmp.h>
  33. #include "config.h"
  34. #include "ctype.h"
  35. #include "vi.h"
  36. #include "regexp.h"
  37.  
  38.  
  39.  
  40. static char    *previous;    /* the previous regexp, used when null regexp is given */
  41.  
  42.  
  43. #ifndef NO_MAGIC
  44. /* THE REAL REGEXP PACKAGE IS USED UNLESS "NO_MAGIC" IS DEFINED */
  45.  
  46. /* These are used to classify or recognize meta-characters */
  47. #define META        '\0'
  48. #define BASE_META(m)    ((m) - 256)
  49. #define INT_META(c)    ((c) + 256)
  50. #define IS_META(m)    ((m) >= 256)
  51. #define IS_CLASS(m)    ((m) >= M_CLASS(0) && (m) <= M_CLASS(9))
  52. #define IS_START(m)    ((m) >= M_START(0) && (m) <= M_START(9))
  53. #define IS_END(m)    ((m) >= M_END(0) && (m) <= M_END(9))
  54. #define IS_CLOSURE(m)    ((m) >= M_SPLAT && (m) <= M_RANGE)
  55. #define ADD_META(s,m)    (*(s)++ = META, *(s)++ = BASE_META(m))
  56. #define GET_META(s)    (*(s) == META ? INT_META(*++(s)) : *s)
  57.  
  58. /* These are the internal codes used for each type of meta-character */
  59. #define M_BEGLINE    256        /* internal code for ^ */
  60. #define M_ENDLINE    257        /* internal code for $ */
  61. #define M_BEGWORD    258        /* internal code for \< */
  62. #define M_ENDWORD    259        /* internal code for \> */
  63. #define M_ANY        260        /* internal code for . */
  64. #define M_SPLAT        261        /* internal code for * */
  65. #define M_PLUS        262        /* internal code for \+ */
  66. #define M_QMARK        263        /* internal code for \? */
  67. #define M_RANGE        264        /* internal code for \{ */
  68. #define M_CLASS(n)    (265+(n))    /* internal code for [] */
  69. #define M_START(n)    (275+(n))    /* internal code for \( */
  70. #define M_END(n)    (285+(n))    /* internal code for \) */
  71.  
  72. /* These are used during compilation */
  73. static int    class_cnt;    /* used to assign class IDs */
  74. static int    start_cnt;    /* used to assign start IDs */
  75. static int    end_stk[NSUBEXP];/* used to assign end IDs */
  76. static int    end_sp;
  77. static char    *retext;    /* points to the text being compiled */
  78.  
  79. /* error-handling stuff */
  80. jmp_buf    errorhandler;
  81. #define FAIL(why)    regerror(why); longjmp(errorhandler, 1)
  82.  
  83.  
  84.  
  85.  
  86.  
  87. /* This function builds a bitmap for a particular class */
  88. static char *makeclass(text, bmap)
  89.     REG char    *text;    /* start of the class */
  90.     REG char    *bmap;    /* the bitmap */
  91. {
  92.     REG int        i;
  93.     int        complement = 0;
  94.  
  95.  
  96.     /* zero the bitmap */
  97.     for (i = 0; bmap && i < 32; i++)
  98.     {
  99.         bmap[i] = 0;
  100.     }
  101.  
  102.     /* see if we're going to complement this class */
  103.     if (*text == '^')
  104.     {
  105.         text++;
  106.         complement = 1;
  107.     }
  108.  
  109.     /* add in the characters */
  110.     while (*text && *text != ']')
  111.     {
  112.         /* is this a span of characters? */
  113.         if (text[1] == '-' && text[2])
  114.         {
  115.             /* spans can't be backwards */
  116.             if (text[0] > text[2])
  117.             {
  118.                 FAIL("Backwards span in []");
  119.             }
  120.  
  121.             /* add each character in the span to the bitmap */
  122.             for (i = text[0]; bmap && i <= text[2]; i++)
  123.             {
  124.                 bmap[i >> 3] |= (1 << (i & 7));
  125.             }
  126.  
  127.             /* move past this span */
  128.             text += 3;
  129.         }
  130.         else
  131.         {
  132.             /* add this single character to the span */
  133.             i = *text++;
  134.             if (bmap)
  135.             {
  136.                 bmap[i >> 3] |= (1 << (i & 7));
  137.             }
  138.         }
  139.     }
  140.  
  141.     /* make sure the closing ] is missing */
  142.     if (*text++ != ']')
  143.     {
  144.         FAIL("] missing");
  145.     }
  146.  
  147.     /* if we're supposed to complement this class, then do so */
  148.     if (complement && bmap)
  149.     {
  150.         for (i = 0; i < 32; i++)
  151.         {
  152.             bmap[i] = ~bmap[i];
  153.         }
  154.     }
  155.  
  156.     return text;
  157. }
  158.  
  159.  
  160.  
  161.  
  162. /* This function gets the next character or meta character from a string.
  163.  * The pointer is incremented by 1, or by 2 for \-quoted characters.  For [],
  164.  * a bitmap is generated via makeclass() (if re is given), and the
  165.  * character-class text is skipped.
  166.  */
  167. static int gettoken(sptr, re)
  168.     char    **sptr;
  169.     regexp    *re;
  170. {
  171.     int    c;
  172.  
  173.     c = **sptr;
  174.     ++*sptr;
  175.     if (c == '\\')
  176.     {
  177.         c = **sptr;
  178.         ++*sptr;
  179.         switch (c)
  180.         {
  181.           case '<':
  182.             return M_BEGWORD;
  183.  
  184.           case '>':
  185.             return M_ENDWORD;
  186.  
  187.           case '(':
  188.             if (start_cnt >= NSUBEXP)
  189.             {
  190.                 FAIL("Too many \\(s");
  191.             }
  192.             end_stk[end_sp++] = start_cnt;
  193.             return M_START(start_cnt++);
  194.  
  195.           case ')':
  196.             if (end_sp <= 0)
  197.             {
  198.                 FAIL("Mismatched \\)");
  199.             }
  200.             return M_END(end_stk[--end_sp]);
  201.  
  202.           case '*':
  203.             return (*o_magic ? c : M_SPLAT);
  204.  
  205.           case '.':
  206.             return (*o_magic ? c : M_ANY);
  207.  
  208.           case '+':
  209.             return M_PLUS;
  210.  
  211.           case '?':
  212.             return M_QMARK;
  213. #ifndef CRUNCH
  214.           case '{':
  215.             return M_RANGE;
  216. #endif
  217.           default:
  218.             return c;
  219.         }
  220.     }
  221.     else if (*o_magic)
  222.     {
  223.         switch (c)
  224.         {
  225.           case '^':
  226.             if (*sptr == retext + 1)
  227.             {
  228.                 return M_BEGLINE;
  229.             }
  230.             return c;
  231.  
  232.           case '$':
  233.             if (!**sptr)
  234.             {
  235.                 return M_ENDLINE;
  236.             }
  237.             return c;
  238.  
  239.           case '.':
  240.             return M_ANY;
  241.  
  242.           case '*':
  243.             return M_SPLAT;
  244.  
  245.           case '[':
  246.             /* make sure we don't have too many classes */
  247.             if (class_cnt >= 10)
  248.             {
  249.                 FAIL("Too many []s");
  250.             }
  251.  
  252.             /* process the character list for this class */
  253.             if (re)
  254.             {
  255.                 /* generate the bitmap for this class */
  256.                 *sptr = makeclass(*sptr, re->program + 1 + 32 * class_cnt);
  257.             }
  258.             else
  259.             {
  260.                 /* skip to end of the class */
  261.                 *sptr = makeclass(*sptr, (char *)0);
  262.             }
  263.             return M_CLASS(class_cnt++);
  264.  
  265.           default:
  266.             return c;
  267.         }
  268.     }
  269.     else    /* unquoted nomagic */
  270.     {
  271.         switch (c)
  272.         {
  273.           case '^':
  274.             if (*sptr == retext + 1)
  275.             {
  276.                 return M_BEGLINE;
  277.             }
  278.             return c;
  279.  
  280.           case '$':
  281.             if (!**sptr)
  282.             {
  283.                 return M_ENDLINE;
  284.             }
  285.             return c;
  286.  
  287.           default:
  288.             return c;
  289.         }
  290.     }
  291.     /*NOTREACHED*/
  292. }
  293.  
  294.  
  295.  
  296.  
  297. /* This function calculates the number of bytes that will be needed for a
  298.  * compiled RE.  Its argument is the uncompiled version.  It is not clever
  299.  * about catching syntax errors; that is done in a later pass.
  300.  */
  301. static unsigned calcsize(text)
  302.     char        *text;
  303. {
  304.     unsigned    size;
  305.     int        token;
  306.  
  307.     retext = text;
  308.     class_cnt = 0;
  309.     start_cnt = 1;
  310.     end_sp = 0;
  311.     size = 5;
  312.     while ((token = gettoken(&text, (regexp *)0)) != 0)
  313.     {
  314.         if (IS_CLASS(token))
  315.         {
  316.             size += 34;
  317.         }
  318. #ifndef CRUNCH
  319.         else if (token == M_RANGE)
  320.         {
  321.             size += 4;
  322.             while ((token = gettoken(&text, (regexp *)0)) != 0
  323.                 && token != '}')
  324.             {
  325.             }
  326.             if (!token)
  327.             {
  328.                 return size;
  329.             }
  330.         }
  331. #endif
  332.         else if (IS_META(token))
  333.         {
  334.             size += 2;
  335.         }
  336.         else
  337.         {
  338.             size++;
  339.         }
  340.     }
  341.  
  342.     return size;
  343. }
  344.  
  345.  
  346.  
  347. /* This function compiles a regexp. */
  348. regexp *regcomp(exp)
  349.     char        *exp;
  350. {
  351.     int        needfirst;
  352.     unsigned    size;
  353.     int        token;
  354.     int        peek;
  355.     char        *build;
  356.     regexp        *re;
  357. #ifndef CRUNCH
  358.     int        from;
  359.     int        to;
  360.     int        digit;
  361. #endif
  362.  
  363.  
  364.     /* prepare for error handling */
  365.     re = (regexp *)0;
  366.     if (setjmp(errorhandler))
  367.     {
  368.         if (re)
  369.         {
  370.             free(re);
  371.         }
  372.         return (regexp *)0;
  373.     }
  374.  
  375.     /* if an empty regexp string was given, use the previous one */
  376.     if (*exp == 0)
  377.     {
  378.         if (!previous)
  379.         {
  380.             FAIL("No previous RE");
  381.         }
  382.         exp = previous;
  383.     }
  384.     else /* non-empty regexp given, so remember it */
  385.     {
  386.